package org.learn.java.mq.listener;

import java.io.UnsupportedEncodingException;

import org.learn.java.mq.consumer.ConsumerTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;

public class MyMessgeListener implements MessageListener {
	public static Logger logger = LoggerFactory.getLogger(ConsumerTest.class);

	public Action consume(Message message, ConsumeContext context) {
		logger.debug("message:" + message);
		logger.debug("body:" + massegeBytToString(message.getBody()));
		logger.debug("context:" + context);
		return Action.CommitMessage;
	}

	/**
	 * 字节数组转为字符串
	 * @param	byte[]
	 * @return	String
	 */
	public String massegeBytToString(byte[] message) {
		String result = null;
		try {
			result = new String(message, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			logger.error("----hi,blue6! 异常错误----");
		}
		return result;
	}
}
